//
//  JwTabBarController.m
//  JwPack
//
//  Created by 陈警卫 on 2022/8/29.
//

#import "JwTabBarController.h"
#import "JwNavigationController.h"

@implementation JwTabBarModel

+ (JwTabBarModel *)modelWithVC:(UIViewController *)vc
                  tabImageName:(NSString *)tabImageName
                         title:(NSString *)title {
    JwTabBarModel *model = [self modelWithVC:vc tabImageName:tabImageName tabSelImageName:nil title:title];
    model.isUseGVColorRender = YES;
    return model;
}

+ (JwTabBarModel *)modelWithVC:(UIViewController *)vc
                  tabImageName:(NSString *)tabImageName
               tabSelImageName:(NSString *)tabSelImageName
                         title:(NSString *)title {
    JwTabBarModel *model = [[JwTabBarModel alloc] init];
    model.vc = vc;
    model.tabImageName = tabImageName;
    model.tabSelImageName = tabSelImageName;
    model.title = title;
    return model;
}

@end

@implementation JwTabBarNavModel

@end

@interface JwTabBarController ()

@property (nonatomic, strong) NSMutableArray *tbnModels;

@end

@implementation JwTabBarController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    kJwWeak(self)
    self.view.jw_didSkinerBlock = ^(id  _Nonnull sender) {
        [wself jw_skinerBlockAction:sender];
    };
}

/// MARK: 全局项目刷新回调
/// 解决 jw_skinerBlockAction 父类子类实现冲突
- (void)jw_skinerBlockAction:(id)sender {
   
    [self setupTabBarAppearance];
    [self refreshTabBarItem];
}

/// 刷新 tabBarItem
- (void)refreshTabBarItem {
    if (!self.tbnModels) {
        return;
    }
    for (JwTabBarNavModel *tbnModel in self.tbnModels) {
        JwNavigationController *nc = tbnModel.nc;
        JwTabBarModel *tabarModel = tbnModel.tabarModel;
        
        UIImage *image = [UIImage jw_imageSkinWithName:tabarModel.tabImageName];
        UIImage *selectedImage = [UIImage jw_imageSkinWithName:tabarModel.tabSelImageName];
        if (!tabarModel.tabSelImageName) {
            selectedImage = image;
        }
        if (tabarModel.isUseGVColorRender) {
            image = [[image jw_imageByTintColor:kJwColorHexString(GV_JwFrame_tabUnSelColorString)] jw_renderOriginal];
            selectedImage = [[selectedImage jw_imageByTintColor:kJwColorHexString(GV_JwFrame_tabSelColorString)] jw_renderOriginal];
        }
        
        nc.tabBarItem.image = image;
        nc.tabBarItem.selectedImage = selectedImage;
        
        nc.title = kJwLocalizedString(tabarModel.title);
        tabarModel.vc.title = kJwLocalizedString(tabarModel.title);
    }
}

/// 添加子控制器
- (void)setupViewControllers:(NSArray *)tabBarModels {
    self.tbnModels = [NSMutableArray array];
    
    NSMutableArray *ncs = [NSMutableArray array];
    for (JwTabBarModel *model in tabBarModels) {
        JwNavigationController *nc = [self navWithRootVc:model.vc
                                                   title:model.title
                                            tabImageName:model.tabImageName
                                         tabSelImageName:model.tabSelImageName
                                             colorRender:model.isUseGVColorRender];
        [ncs addObject:nc];
        
        JwTabBarNavModel *tbnModel = [[JwTabBarNavModel alloc] init];
        tbnModel.nc = nc;
        tbnModel.tabarModel = model;
        [self.tbnModels addObject:tbnModel];
    }
    self.viewControllers = ncs;
}

- (JwNavigationController *)navWithRootVc:(UIViewController *)vc
                                    title:(NSString *)title
                             tabImageName:(NSString *)tabImageName
                          tabSelImageName:(NSString *)tabSelImageName
                              colorRender:(BOOL)colorRender {
    JwNavigationController *nc = [[JwNavigationController alloc] initWithRootViewController:vc];
    
    UIImage *image = [UIImage jw_imageSkinWithName:tabImageName];
    UIImage *selectedImage = [UIImage jw_imageSkinWithName:tabSelImageName];
    if (!tabSelImageName) {
        selectedImage = image;
    }
    if (colorRender) {
        image = [[image jw_imageByTintColor:kJwColorHexString(GV_JwFrame_tabUnSelColorString)] jw_renderOriginal];
        selectedImage = [[selectedImage jw_imageByTintColor:kJwColorHexString(GV_JwFrame_tabSelColorString)] jw_renderOriginal];
    }
    
    nc.tabBarItem.image = image;
    nc.tabBarItem.selectedImage = selectedImage;
    
    nc.title = kJwLocalizedString(title);
    vc.title = kJwLocalizedString(title);
    
    return nc;
}

- (void)setupTabBarAppearance {
    UIColor *tintColor = self.jw_tabTintColor;
    UIColor *selColor = self.jw_tabSelColor;
    UIColor *unSelColor = self.jw_tabUnSelColor;
    
    if (!tintColor) {
        tintColor = [UIColor jw_colorHexString:GV_JwFrame_tabTintColorString];
    }
    if (!selColor) {
        selColor = [UIColor jw_colorHexString:GV_JwFrame_tabSelColorString];
    }
    if (!unSelColor) {
        unSelColor = [UIColor jw_colorHexString:GV_JwFrame_tabUnSelColorString];
    }
    
    [self setupTabBarAppearanceWithTintColor:tintColor selColor:selColor unSelColor:unSelColor];
}

- (void)setupTabBarAppearanceWithTintColor:(UIColor *)tintColor selColor:(UIColor *)selColor unSelColor:(UIColor *)unSelColor {
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        [appearance configureWithTransparentBackground];
        appearance.shadowColor = [UIColor clearColor];
        appearance.backgroundColor = tintColor;
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName : unSelColor};
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName : selColor};
        self.tabBar.standardAppearance = appearance;
        if(@available(iOS 15.0,*)){
            self.tabBar.scrollEdgeAppearance = appearance;
        }
    } else {
        self.tabBar.translucent = YES;
        self.tabBar.barTintColor = tintColor;
        self.tabBar.tintColor = selColor;
        self.tabBar.unselectedItemTintColor = unSelColor;
        // 去除顶部横线
        [self.tabBar setBackgroundImage:[UIImage new]];
        [self.tabBar setShadowImage:[UIImage new]];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
